package com.salton123.plugin

import com.android.build.api.transform.DirectoryInput
import com.android.build.api.transform.JarInput
import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform
import com.android.build.api.transform.TransformInvocation
import com.android.build.gradle.internal.pipeline.TransformManager
import com.salton123.plugin.util.log
import org.gradle.api.Project

//ModuleInitTransform
class RoomTransform(project: Project) : Transform() {
    private val TAG = "RoomTransform"
    override fun getName(): String = TAG
    override fun getInputTypes(): MutableSet<QualifiedContent.ContentType> = TransformManager.CONTENT_JARS
    override fun isIncremental(): Boolean = true
    override fun getScopes(): MutableSet<in QualifiedContent.Scope> = TransformManager.SCOPE_FULL_PROJECT

    override fun transform(transformInvocation: TransformInvocation?) {
        log("--------------- RoomTransform start ---------------")
        val startTime = System.currentTimeMillis()
        //删除之前的输出
        transformInvocation?.outputProvider?.deleteAll()
        transformInvocation?.inputs?.forEach {
            it.directoryInputs.forEach { inputs ->
                handleDirectoryInput(inputs)
            }
            it.jarInputs.forEach { inputs ->
                handleJarInput(inputs)
            }
        }

        //找出实现类名字
        //添加到实现中
        log("--------------- RoomTransform end ---------------")
        val cost = (System.currentTimeMillis() - startTime) / 1000
        log("LifecyclePlugin cost ： $cost s")
//        val helper = ThreadAsmHelper()
//        val baseTransform = BaseTransform(transformInvocation, object : TransformCallBack {
//            override fun process(className: String, classBytes: ByteArray?): ByteArray? {
//                return if (ClassUtils.checkClassName(className)) {
//                    helper.modifyClass(classBytes)
//                } else {
//                    null
//                }
//            }
//        })
//        baseTransform.startTransform()
    }

    private fun handleJarInput(inputs: JarInput?) {
        log("dir:${inputs?.file}")
        inputs?.file?.listFiles()?.forEach {
            log("className:${it.name}")
        }
    }

    private fun handleDirectoryInput(dir: DirectoryInput) {
        log("dir:${dir.file}")
        if (dir.file?.isDirectory!!) {
            dir.file?.listFiles()?.forEach {
                log("className:${it.name}")
//                if (it.name == "com/salton123/app/BaseApplication.class") {
//                    val classReader = ClassReader(it.readBytes())
//                    val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
//                    var classVisitor = LifeCycleClassVisitor(classWriter)
//                    classReader.accept(classVisitor, ClassReader.EXPAND_FRAMES)
//                    val fos = FileOutputStream(it.absolutePath)
//                    fos.write(classWriter.toByteArray())
//                    fos.close()
//                }
            }
        }
        //处理完输入文件之后，要把输出给下一个任务
    }
}
